package pers.hl.module_main.explore

import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.NonNull
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.FragmentUtils
import com.blankj.utilcode.util.LogUtils
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.youth.banner.Banner
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator
import pers.hl.lib_global.common.GlideUtils
import pers.hl.lib_global.model.bean.Book
import pers.hl.lib_global.model.bean.BookTag
import pers.hl.lib_global.model.bean.Library
import pers.hl.module_main.R
import pers.hl.module_main.common.PageHelper
import pers.hl.module_main.databinding.ItemExploreBookItemBinding
import pers.hl.module_main.databinding.ItemHomeRvTitleBinding
import pers.hl.module_main.mine.TagFragment

class ExploreAdapter(@NonNull private val fragment: ExploreFragment
) : BaseMultiItemQuickAdapter<ExploreLayoutBean, BaseViewHolder>() {

    init {
        addItemType(ExploreLayoutBean.TYPE_RV_TITLE, R.layout.item_home_rv_title)
        addItemType(ExploreLayoutBean.TYPE_BANNER, R.layout.item_recommend_banner)
        addItemType(ExploreLayoutBean.TYPE_RV_CONTENT_BOOK, R.layout.item_explore_book_rv_hor)
    }

    @Suppress("UNCHECKED_CAST")
    override fun convert(holder: BaseViewHolder, item: ExploreLayoutBean) {
        when (holder.itemViewType) {
            ExploreLayoutBean.TYPE_RV_TITLE -> {
                val data: BookTag = item.getData() as BookTag
                showRVTitle(holder, data)
            }
            ExploreLayoutBean.TYPE_LIBRARY -> {
                val data = item.getData() as Library
                showNearbyLibraries(data)
            }
            ExploreLayoutBean.TYPE_BANNER -> {
                val data: List<Book> = item.getData() as List<Book>
                showRecommendBanner(holder, data)
            }
            ExploreLayoutBean.TYPE_CATEGORY -> {
                val data = item.getData()
                showCategories(data as Book)
            }
            ExploreLayoutBean.TYPE_RV_CONTENT_BOOK -> {
                val data: List<Book> = item.getData() as List<Book>
                showBooks(holder, data)
            }
        }
    }

    private fun showBooks(holder: BaseViewHolder, data: List<Book>) {
        val recyclerView = holder.getView<RecyclerView>(R.id.rvExploreBook)
        recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
        recyclerView.adapter = object : BaseQuickAdapter<Book, BaseViewHolder>(R.layout.item_explore_book_item, data.toMutableList()) {
            override fun convert(holder: BaseViewHolder, item: Book) {
                val binding = ItemExploreBookItemBinding.bind(holder.itemView)
                binding.apply {
                    tvRecBookTitle.text = item.bookName
                    GlideUtils.loadBookImage(ivRecBook.context, ivRecBook, item)
                    root.setOnClickListener {
                        PageHelper.startBookDetailPage(root.context, item)
                    }
                }
            }
        }
    }

    private fun showRVTitle(holder: BaseViewHolder, tag: BookTag) {
        val binding = ItemHomeRvTitleBinding.bind(holder.itemView)
        binding.apply {
            tvRvTitle.text = tag.name
            tvMore.setOnClickListener {
                LogUtils.d("tvMore.setOnClickListener")
                FragmentUtils.add(fragment.parentFragmentManager, TagFragment.newInstance(tag), R.id.fl_container, false, true)
            }
            root.setOnClickListener {
                LogUtils.d("root.setOnClickListener")
                FragmentUtils.add(fragment.parentFragmentManager, TagFragment.newInstance(tag), R.id.fl_container, false, true)
            }
        }
    }

    private fun showNearbyLibraries(library: Library) {

    }

    private fun showCategories(book: Book) {
    }

    private fun showRecommendBanner(holder: BaseViewHolder, data: List<Book>) {
        val banner = holder.itemView as Banner<Book, BannerImageAdapter<Book>>
        banner.setAdapter(object : BannerImageAdapter<Book>(data) {
            override fun onBindView(
                holder: BannerImageHolder,
                data: Book,
                position: Int,
                size: Int
            ) {
                holder.itemView.let {
                    GlideUtils.loadBookImage(it.context, it as ImageView, data)
                }
                holder.imageView.apply {
                    setOnClickListener {
                        PageHelper.startBookDetailPage(it.context, data)
                    }
                }
            }
        }).addBannerLifecycleObserver(fragment).indicator =
            CircleIndicator(holder.itemView.context)
        banner.setBannerRound(20f)
    }

}